import { BasicColumn, BasicTable, FormSchema, TableAction, useTable } from "@/components/Table";
import { Tag } from "antd";
import Details from "./components/Details";
import { useDrawer } from "@/components/Drawer";
import { useTranslation } from "react-i18next";
import { useEffect, useState } from "react";
import { getListSimpleUsers } from "@/api/system/user";
import { IconEnum } from "@/enums/appEnum";
import { getPatientsPage } from "@/api/components/patientInformation";

const index = () => {
	const { t } = useTranslation();
	const en_vi = "Hospitalization.Hospitalization.depositHistory";
	const [userList, setUserList] = useState<any>([]);
	const schemas: FormSchema[] = [
		{
			label: t(`${en_vi}.fullName`),
			field: "fullName",
			component: "Input"
		},
		{
			label: t(`${en_vi}.gender`),
			field: "gender",
			component: "Input"
		},
		{
			label: t(`${en_vi}.citizenId`),
			field: "citizenId",
			component: "Input"
		},
		{
			label: t(`${en_vi}.patCardno`),
			field: "patCardno",
			component: "Input"
		},
		{
			label: t(`erp.warehouses.status`),
			field: "status",
			component: "Select",
			componentProps: {
				options: [
					{
						label: t("erp.warehouseGroupType.open"),
						value: "0"
					},
					{
						label: t("erp.warehouseGroupType.close"),
						value: "1"
					}
				]
			}
		}
	];
	const columns: BasicColumn[] = [
		{
			title: t(`${en_vi}.fullName`),
			dataIndex: "fullName"
		},
		{
			title: t(`${en_vi}.gender`),
			dataIndex: "gender"
		},
		{
			title: t(`${en_vi}.ethnicity`),
			dataIndex: "ethnicity"
		},
		{
			title: t(`${en_vi}.birthday`),
			dataIndex: "birthday"
		},
		{
			title: t(`${en_vi}.citizenId`),
			dataIndex: "citizenId"
		},
		{
			title: t(`${en_vi}.patCardno`),
			dataIndex: "patCardno"
		},
		{
			title: t(`${en_vi}.nationality`),
			dataIndex: "nationality"
		},
		{
			title: t(`${en_vi}.phoneNumb`),
			dataIndex: "phoneNumb"
		},
		{
			title: t(`${en_vi}.mobileNumb`),
			dataIndex: "mobileNumb"
		},
		{
			title: t(`${en_vi}.emailAddr`),
			dataIndex: "emailAddr"
		},
		{
			title: t(`${en_vi}.address`),
			dataIndex: "addrProvince",
			render: (_: any, record: any) => {
				if (record.addrWard)
					return record.addrProvince + record.addrCity + record.addrDistrict + record.addrCommune + record.addrWard;
				if (record.addrCommune) return record.addrProvince + record.addrCity + record.addrDistrict + record.addrCommune;
				if (record.addrDistrict) return record.addrProvince + record.addrCity + record.addrDistrict;
				if (record.addrCity) return record.addrProvince + record.addrCity;
				if (record.addrProvince) return record.addrProvince;
				return "";
			}
		},
		{
			title: t(`erp.warehouses.status`),
			dataIndex: "status",
			render: (status: any) => {
				return status === "0" ? (
					<Tag color="success">{t("task.processExpression.open")}</Tag>
				) : (
					<Tag color="default">{t("task.processExpression.closed")}</Tag>
				);
			}
		},
		{
			title: t(`erp.warehouseGroupType.operations`),
			fixed: "right",
			width: 200,
			render: (_: any, record: any) => (
				<TableAction
					actions={[
						{
							icon: IconEnum.EDIT,
							label: "details",
							onClick: details(record)
						}
					]}
				></TableAction>
			)
		}
	];
	const [registerTable, { reload }] = useTable({
		api: getPatientsPage,
		columns,
		useSearchForm: true,
		formConfig: {
			labelWidth: 120,
			schemas,
			showActionButtonGroup: true,
			showAdvancedButton: false,
			baseColProps: { span: 6 },
			actionColOptions: { span: 24 }
		},
		bordered: true
	});
	const [registerDrawer, { openDrawer }] = useDrawer();
	const details = (record: any) => async () => {
		openDrawer(true, {
			reload,
			record
		});
	};
	const getUseList = async () => {
		const res = await getListSimpleUsers();
		setUserList(res);
		console.log(userList);
	};
	useEffect(() => {
		getUseList();
	}, []);
	return (
		<div>
			<div className="page-content">
				<BasicTable register={registerTable}></BasicTable>
			</div>
			<Details register={registerDrawer}></Details>
		</div>
	);
};

export default index;
